package com.yhjr.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yhjr.pojo.Customer_basic;
import com.yhjr.service.inter.Customer_basicService;
import com.yhjr.service.inter.Customer_caselistService;
import com.yhjr.service.inter.Customer_main_eService;
import com.yhjr.service.inter.Customer_main_pService;
import com.yhjr.service.inter.Customer_propertyService;
import com.yhjr.service.inter.Customer_vehicleService;
import com.yhjr.util.GlobalConstant;
import com.yhjr.util.Json;
import com.yhjr.util.SessionInfo;


/**
 * 用户基本信息的控制层
 * @author Administrator
 *
 */

@RequestMapping("/customer_basicController")

@Controller
public class Customer_basicController {

	
	@Autowired
	private   Customer_basicService     customer_basicService;
	@Autowired
	private   Customer_caselistService    customer_caselistService;
	@Autowired
	private   Customer_main_eService     customer_main_eService;
	@Autowired
	private   Customer_main_pService   customer_main_pService;
	@Autowired
	private   Customer_propertyService   customer_propertyService;
	@Autowired
	private   Customer_vehicleService     customer_vehicleService;

	
	
	/**
	 * 获得用户资料列表
	 * 
	 * @return
	 */
	@RequestMapping("treeGrid")
	public ModelAndView treeGrid() {
		// 创建ModelAndView对象
		ModelAndView mv = new ModelAndView();
		// 保存模型数据
		mv.addObject("list", customer_basicService.treeGrid());// 获得物业资料列表
		// 关联视图
		mv.setViewName("/charge/data");
		return mv;
	}
	
	
	
	

	/**
	 * 删除物业资料
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	public String delete(int id) {
		// 调用底层的删除方法
		customer_basicService.delete(id);
		return "redirect:treeGrid";
	}

	
	


	/**
	 * 根据物业id获得物业资料信息
	 * 
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping("/getDataById")
	@ResponseBody
	public Json getDataById(int id) {
		// 创建Json对象
		Json j = new Json();
		try {
			// 根据物业id获得物业资料信息
			/*Map<String, Object>  dataById=new HashMap<String, Object>();
			dataById.put("list1",customer_basicService.getDataById(id));
			dataById.put("list2",customer_main_eService.getDataById(id));
			dataById.put("list3", customer_caselistService.getDataById(id));
			dataById.put("list4",customer_main_pService.getDataById(id));
			dataById.put("list5", customer_propertyService.getDataById(id));
			dataById.put("list6",customer_vehicleService.getDataById(id));*/
			
			Customer_basic dataById = customer_basicService.getDataById(id);
			
			// 设置Json对象的成功值
			j.setObj(dataById);
			j.setSuccess(true);
		} catch (Exception e) {
			// 设置Json对象的失败值
			j.setMsg(e.getMessage());
		}
		return j;
	}

	
	
	
	/**
	 * 修改用户资料
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping("update")
	public Json update(HttpServletRequest request,Customer_basic customer_basic) {
		// 创建Json对象
		Json j = new Json();
		try {
			// 调用底层的修改方法
			customer_basicService.update(customer_basic);
			// 设置Json对象的成功值
			j.setSuccess(true);
			j.setMsg("修改成功！");
		} catch (Exception e) {
			// 设置Json对象的是宝宝值
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	
	/**
	 * 新增用户时显示拼接出的客户编号
	 */
	@RequestMapping("/getCustId")
	@ResponseBody
	public String getCustId(){
		String custId= customer_basicService.getCustId();
		return custId;
	}
	
	
	/**
	 * 添加用户信息
	 */
	@RequestMapping("/add")
	public Json add(HttpServletRequest request, Customer_basic customer_basic) {
		// 获得sessionInfo对象
		SessionInfo sessionInfo = (SessionInfo) request.getSession()
				.getAttribute(GlobalConstant.SESSION_INFO);
		// 创建Json对象
		Json j = new Json();
		try {
			// 添加物业资料
			customer_basicService.save(customer_basic);
			// 设置Json对象的成功值
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			// 设置Json对象的失败值
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	
    /**
     * 根据条件查询所有数据
     */
	
	@RequestMapping("/selectByCondition")
	public  ModelAndView   selectByCondition(Customer_basic   customer_basic){
		
		ModelAndView   mv=new  ModelAndView();
		
		//根据用户
		//判断用户名是否为空
		if(customer_basic.getCustname()!=null && !"".equals(customer_basic.getCustname())){
		//设置值取到值后进行模糊查询
			customer_basic.setCustname("%"+customer_basic.getCustname()+"%");
		}else{
			customer_basic.setCustname(null);
		}
		//根据身份证号码进行查询
		//判断身份证是否为空
		if(customer_basic.getIdentityno()!=null && !"".equals(customer_basic.getIdentityno())){
		//设置值之后在取值时进行模糊查询
			customer_basic.setIdentityno("%"+customer_basic.getIdentityno()+"%");
		}else{
			customer_basic.setIdentityno(null);
		}
	    //根据用户编号查询
		//判断ID是否为空
		if(customer_basic.getSysid()!= null && !"".equals(customer_basic.getSysid())){
    	//设置值后把取到的值进行模糊查询
		customer_basic.setSysid(null);
		}else{
		customer_basic.setSysid(0);
		}
    	 List<Customer_basic> listCustomer = customer_basicService.selectByCondition(customer_basic);
	    mv.addObject("listCustomer",listCustomer);
	    mv.setViewName("/charge/data");
		return  mv;
	}
}
